package com.ihr360.message.service.impl;

import com.ihr360.message.dao.MessageSendRecordDao;
import com.ihr360.message.dao.MessageSendTaskDao;
import com.ihr360.message.dto.MessageSendRecordDto;
import com.ihr360.message.dto.MessageSendTaskDto;
import com.ihr360.message.entity.MessageSendRecord;
import com.ihr360.message.entity.MessageSendTask;
import com.ihr360.message.event.MessageSendTaskEvent;
import com.ihr360.message.service.MessageSendTaskService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author tom
 * @date 17-12-28
 */
@Service
public class MessageSendTaskServiceImpl implements MessageSendTaskService, ApplicationContextAware {
    private final static Logger logger = LoggerFactory.getLogger(MessageSendTaskServiceImpl.class);


    @Autowired
    private MessageSendTaskDao messageSendTaskDao;
    @Autowired
    private MessageSendRecordDao messageSendRecordDao;

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public boolean addSendTask(MessageSendTask messageSendTask) {
        MessageSendTaskDto messageSendTaskDto = new MessageSendTaskDto();
        BeanUtils.copyProperties(messageSendTask, messageSendTaskDto);
        messageSendTaskDto.setMessageList(null);
        messageSendTaskDto.setCreatedAt(new Date());
        messageSendTaskDao.save(messageSendTaskDto);
        List<MessageSendRecordDto> messageSendRecordDtoList = new ArrayList<>();
        messageSendTask.getMessageList().forEach(messageSendRecord -> {
            MessageSendRecordDto messageSendRecordDto = new MessageSendRecordDto();
            BeanUtils.copyProperties(messageSendRecord, messageSendRecordDto);
            messageSendRecordDto.setTaskId(messageSendTaskDto.getId());
            messageSendRecordDto.setCreatedAt(new Date());
            messageSendRecordDto.setFailure_count(0);
            messageSendRecordDto.setSuccess_count(0);
            messageSendRecordDao.saveAndFlush(messageSendRecordDto);
            messageSendRecordDtoList.add(messageSendRecordDto);
        });
//        messageSendRecordDao.save(messageSendRecordDtoList);
        MessageSendTaskEvent messageSendTaskEvent = new MessageSendTaskEvent(applicationContext, messageSendTaskDto.getId());
        applicationContext.publishEvent(messageSendTaskEvent);
        return true;
    }

    @Override
    public Integer getErrorCountBySheetId(String sheetId) {
        Integer errorCount = 0;

        List<MessageSendTaskDto> messageSendTaskDtoList = messageSendTaskDao.findBySheetId(sheetId);

        for (MessageSendTaskDto messageSendTaskDto : messageSendTaskDtoList) {
            long count = messageSendRecordDao.countByTaskIdAndSendStatus(messageSendTaskDto.getId(), MessageSendRecord.SendStatus.SEND_FAILED);
            errorCount = Math.toIntExact(errorCount + count);
        }
        return errorCount;
    }

    @Override
    public Integer getSMSCountBySheetId(String sheetId) {
        Integer smsCount = 0;
        List<MessageSendTaskDto> messageSendTaskDtoList = messageSendTaskDao.findBySheetIdAndMessageType(sheetId, MessageSendTaskDto.MessageType.SMS);

        for (MessageSendTaskDto messageSendTaskDto : messageSendTaskDtoList) {
           smsCount = smsCount + messageSendTaskDto.getMessageList().size();
        }
        return smsCount;
    }
}
